home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / IRC / Scripts / / BotKill / BOTKILL / Source / socket.c < prev    next >
C/C++ Source or Header  |  1993-07-14  |  4KB  |  164 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <time.h>
  7. #include "config.h"
  8. #include "socket.h"
  9.  
  10.  
  11. /* 
  12.    PROCEDURE int get_socket (serv_name, port_num)
  13.    INPUTS: 
  14.       serv_name   string name of a server (e.g. "csa.bu.edu")
  15.       port_num    port number of the server (e.g. 6667)
  16.    EFFECTS:
  17.       establishes a socket stream connection with the server.
  18.       Returns the socket fd if the connection is succesfully established.
  19.       Returns NO_SERVER (-1) if the server named by serv_name does not exist.
  20.       Returns CANT_OPEN_SOCK (-2) if a socket cannot be opened.
  21.       Returns CON_REFUSED (-3) if the procedure can't connect to the server.
  22. */
  23.  
  24. int get_socket (serv_name, port_num)
  25.      char *serv_name;
  26.      unsigned short int port_num;
  27.      
  28. {
  29.   struct hostent *serv_entry;
  30.   struct sockaddr_in serv_addr;
  31.   int sockfd;
  32.  
  33.   /* Get IP address of named server */
  34.   if ( DEBUG )
  35.     printf( "PROC get_socket: getting IP address of server %s\n", serv_name );
  36.   if ( ( serv_entry = gethostbyname ( serv_name ) ) == NULL )
  37.     {
  38.       if ( DEBUG )
  39.     printf( "PROC get_socket: Server does not exist: %s\n", serv_name);
  40.       return NO_SERVER;
  41.     }
  42.  
  43.   /* Initialize socket address of the server */
  44.   if ( DEBUG )
  45.     printf( "PROC get_socket: initializing the socket address of server.\n" );
  46.   serv_addr.sin_family = AF_INET;
  47.   serv_addr.sin_port = htons( port_num );
  48.   bcopy( serv_entry->h_addr, (char *) &( serv_addr.sin_addr ),
  49.       serv_entry->h_length );
  50.  
  51.   /* Establish socket connection */
  52.   if ( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
  53.     {
  54.       if ( DEBUG )
  55.        printf( "PROC get_socket: Socket cannot be opened.\n" );
  56.       return CANT_OPEN_SOCK;
  57.     }
  58.   if ( connect( sockfd, &serv_addr, sizeof serv_addr ) < 0 )
  59.     {
  60.       if ( DEBUG )
  61.     printf( "PROC get_socket: Cannot connect to server: %s\n", serv_name);
  62.       return CON_REFUSED;
  63.     }
  64.   
  65.   if ( DEBUG )
  66.     {
  67.       printf( "PROC get_socket: Successfully connected to server: %s\n", serv_name );
  68.       printf( "PROC get_socket: Socket number is %d\n", sockfd );
  69.     }
  70.   return sockfd;
  71.  
  72.  
  73. /*
  74.   PROCDURE void readln ( sockfd, buff, buffln )
  75.   INPUTS:
  76.      sockfd     the socket fd
  77.      buff       a pointer to a character array for the input
  78.      buffln     the length of the array buff
  79.   MODIFIES:
  80.      buff
  81.   EFFECTS:
  82.      Reads a line from the socket connection identified with sockfd.
  83.      The input is put into the character array buff.  
  84.      Returns the length of the line which was read.
  85.      Returns READ_ERR (-1) if there is an error while reading.
  86. */
  87.  
  88. int readln ( sockfd, buff, buffln )
  89.      int sockfd;
  90.      char *buff;
  91.      int buffln;
  92.  
  93. {
  94.   int count, len_of_line=1;
  95.  
  96.   /* Find out how long the line is */
  97.   if ( recv( sockfd, buff, buffln, MSG_PEEK ) < 1 )
  98.     {
  99.       perror( "PROC readln" );
  100.       return ( -1 ); 
  101.     }
  102.   for( count = 0; ( buff[count] != '\n' ) && ( count < ( buffln - 2 ) );
  103.        count++, len_of_line++ );
  104.  
  105.   /* Now read the line and put an \0 character at the end of it. */
  106.   if ( recv( sockfd, buff, len_of_line, 0 ) < 1 )
  107.     {
  108.       perror( "PROC readln" );
  109.       return( -1 );
  110.     }
  111.  
  112.   buff[len_of_line] = '\0';
  113.  
  114.   return len_of_line;
  115.  
  116. }
  117.  
  118.  
  119. /*
  120.   PROCDURE int writeln ( sockfd, buff, buffln )
  121.   INPUTS:
  122.      sockfd     the socket fd
  123.      buff       a pointer to a character array for the output
  124.      buffln     the length of the string to be printed
  125.   REQUIRES:
  126.      buffln must be smaller than the size of the array buff
  127.   MODIFIES:
  128.      buff
  129.   EFFECTS:
  130.      Writes a line of output to the socket connection with identification
  131.      number sockfd.  The output is read from the array pointed to by buff.
  132.      Returns the length of the line which was written.
  133.      Returns WRITE_ERR (-1) if there is an error while writing.
  134. */
  135.  
  136. int writeln ( sockfd, buff, buffln )
  137.      int sockfd;
  138.      char *buff;
  139.      int buffln;
  140.  
  141. {
  142.   if ( DEBUG )
  143.     {
  144.       printf( "PROC writeln: this is the buffer\n" );
  145.       printf( "PROC writeln: %s", buff );
  146.       printf( "PROC writeln: the bufflen is %d\n", buffln);
  147.       printf( "PROC writeln: the sockfd is %d\n", sockfd);
  148.     }
  149.   if ( send( sockfd, buff, buffln, 0 ) <= 0 )
  150.     {
  151.       if ( DEBUG )
  152.     printf( "PROC writeln: Error in writing line to socket.\n" );
  153.       return ( -1 );
  154.     }
  155.   
  156.   return buffln;
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163.